<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\seo\admin\controller;

use onekey\File;
use think\facade\Db;
use think\facade\View;
use app\index\addons\Url;
use app\admin\model\Config;
use app\admin\model\Catalog;
use app\admin\BaseController;
/**
 * SEO优化
 */
class Seo extends BaseController
{
    public function index()
    {
        $name = 'app_seo';
        if ($this->request->isPost()) {
            $msg = Config::setVal($name, 'SEO设置', input('post.value'));
            return json($msg);
        } else {
            $config = Config::getVal($name);
            if (! $config) {
                $config = [
                    'baidu_url_status'  => 0,
                    'baidu_html_code'   => "",
                    'google_url_status' => 0, 
                    'google_html_code'  => "", 
                ];
            }
            $scheme   = $this->request->scheme().'://';
            $included = str_replace($scheme, '', $this->request->domain());
            $sitemap  = is_file(public_path() . 'sitemap.xml');
            View::assign([
                'config'   => $config,
                'domain'   => $this->request->domain(),
                'sitemap'  => $sitemap,
                'included' => $included,
            ]);
            return View::fetch();
        }
    }

    /**
     * 生成站点地图
     */
    public function sitemap() 
    {
        $date = date('Y-m-d');
        $input = input();
        $catalog = Catalog::field('id,seo_url')->where('theme', theme())->where('status',1)->where('links_type', 0)->order('sort','desc')->select()->toArray();
        // 生成sitemap
        $sitemapXml = ""; 
        $sitemapXml .= "<?xml version='1.0' encoding='UTF-8'?>\r\n";
        $sitemapXml .= "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\r\n";
        // 分类页
        foreach ($catalog as $key => $val) {
            $loc = Url::catalog($val);
            if (!empty($loc)) {
                $sitemapXml .= "<url>\n";
                $sitemapXml .= "    <loc>$loc</loc>\n";
                $sitemapXml .= "    <priority>1</priority>\n";
                $sitemapXml .= "    <lastmod>$date</lastmod>\n";
                $sitemapXml .= "    <changefreq>weekly</changefreq>\n";
                $sitemapXml .= "</url>\n";
            }
        }
        // 详情页
        foreach (plugin_list() as $rkey => $route) {
            foreach ($route['route'] as $key => $val) {
                $exist = Db::query("SHOW TABLES LIKE 'mk_".$val['table']."'");
                if ($exist) {
                    $single = Db::name($val['table'])->select()->toArray();
                    foreach ($single as $k => $v) {
                        if (isset($v['catalog_id'])) {
                            $loc = Url::single($v);
                            if (! empty($loc)) {
                                $sitemapXml .= "<url>\n";
                                $sitemapXml .= "    <loc>$loc</loc>\n";
                                $sitemapXml .= "    <priority>0.8</priority>\n";
                                $sitemapXml .= "    <lastmod>$date</lastmod>\n";
                                $sitemapXml .= "    <changefreq>weekly</changefreq>\n";
                                $sitemapXml .= "</url>\n";
                            }
                        }
                    }
                }
            }
        }
        $sitemapXml .= "</urlset>";
        File::create(public_path() . 'sitemap.xml', $sitemapXml);
        File::create(public_path() . 'robots.txt', "User-agent: *\nDisallow:/public/\nSitemap: ".$this->request->domain());
        return json(['status' => 'success', 'message' => '文件创建成功']);
    }
}